package com.blb.zy_community.mapper;

import com.blb.zy_community.entity.SysDept;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * <p>
 * 部门表 Mapper 接口
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@Mapper
public interface SysDeptMapper extends BaseMapper<SysDept> {
    /**
     * 公司三级树形图查询
     * @return
     */
    @Results(id = "selectDeptTreeMap",
            value = {
                    @Result(property = "deptId", column = "dept_id", id = true),
                    @Result(property = "parentId", column = "parent_id"),
                    @Result(property = "ancestors", column = "ancestors"),
                    @Result(property = "deptName", column = "dept_name"),
                    @Result(property = "orderNum", column = "order_num"),
                    @Result(property = "leader", column = "leader"),
                    @Result(property = "phone", column = "phone"),
                    @Result(property = "email", column = "email"),
                    @Result(property = "status", column = "status"),
                    @Result(property = "delFlag", column = "del_flag"),
                    @Result(property = "createBy", column = "create_by"),
                    @Result(property = "createTime", column = "create_time"),
                    @Result(property = "updateBy", column = "update_by"),
                    @Result(property = "updateTime", column = "update_time"),
                    @Result(property = "childDept", column = "dept_id", javaType = List.class,
                            many = @Many(select = "com.blb.zy_community.mapper.SysDeptMapper.selectChildDeptTreeByParentId"))
            })
    @Select("select * from sys_dept where parent_id = #{parentId}")
    List<SysDept> selectChildDeptTreeByParentId(Long parentId);

    @Select("select * from sys_dept where parent_id = #{parentId}")
    List<SysDept> selectChildDeptByParentId(Long parentId);

//
//    <where>
//            <if test="userId != null and userId != ''">
//    and u.user_id like concat("%",#{userId},"%")
//            </if>
//        </where>


    @Results(id = "selectDeptTreeMap2",
            value = {
                    @Result(property = "deptId", column = "dept_id", id = true),
                    @Result(property = "parentId", column = "parent_id"),
                    @Result(property = "ancestors", column = "ancestors"),
                    @Result(property = "deptName", column = "dept_name"),
                    @Result(property = "orderNum", column = "order_num"),
                    @Result(property = "leader", column = "leader"),
                    @Result(property = "phone", column = "phone"),
                    @Result(property = "email", column = "email"),
                    @Result(property = "status", column = "status"),
                    @Result(property = "delFlag", column = "del_flag"),
                    @Result(property = "createBy", column = "create_by"),
                    @Result(property = "createTime", column = "create_time"),
                    @Result(property = "updateBy", column = "update_by"),
                    @Result(property = "updateTime", column = "update_time"),
                    @Result(property = "childDept", column = "{parentId=dept_id,deptName=my_dept_name,status=my_status}", javaType = List.class,
                            many = @Many(select = "com.blb.zy_community.mapper.SysDeptMapper.selectByDept"))
            })
    @Select("<script>" +
            " select sys_dept.*,#{deptName} my_dept_name,#{status} my_status from sys_dept " +
            "<where>" +
            "<if test=\"deptName != null and deptName != ''\"> " +
            " and dept_name like concat('%',#{deptName},'%') " +
            "</if> " +
            "<if test=\"status != null and status != ''\">" +
            " and status like concat('%',#{status},'%') " +
            "</if> " +
            " and parent_id = #{parentId} " +
            "</where>" +
            "</script> ")
    List<SysDept> selectByDept(Long parentId, String deptName, String status);

}
